﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using GenericEntities;
using System.Windows.Forms;

namespace DiaryPlanner
{
    class CalendarThread
    {
        Planner planner;
        PlannerDay plannerDay;
        PlannerWeek plannerWeek;
        PlannerMonth plannerMonth;
        GenericEvent ge, oldGe,newGe;

        private const string playing = "... 연동 중 ...";
        private const string complete = "... 연동 완료 ...";
        private const string nullstr = "";

        public CalendarThread(Planner planner, PlannerDay plannerDay, PlannerWeek plannerWeek, PlannerMonth plannerMonth)
        {
            this.planner = planner;
            this.plannerDay = plannerDay;
            this.plannerWeek = plannerWeek;
            this.plannerMonth = plannerMonth;
        }

        public CalendarThread(Planner planner, GenericEvent ge)
        {
            this.planner = planner;
            this.ge = ge;
        }

        public CalendarThread(Planner planner,PlannerDay plannerDay, GenericEvent ge)
        {
            this.planner = planner;
            this.plannerDay = plannerDay;
            this.ge = ge;
            initCalendarThread();
        }

        public CalendarThread(Planner planner,PlannerWeek plannerWeek, GenericEvent ge)
        {
            this.planner = planner;
            this.plannerWeek = plannerWeek;
            this.ge = ge;
            initCalendarThread();
        }

        public CalendarThread(Planner planner,PlannerMonth plannerMonth, GenericEvent ge)
        {
            this.planner = planner;
            this.plannerMonth = plannerMonth;
            this.ge = ge;
            initCalendarThread();
        }

        public CalendarThread(Planner planner, GenericEvent oldGe, GenericEvent newGe)
        {
            this.oldGe = oldGe;
            this.newGe = newGe;
            initCalendarThread();
        }

        public CalendarThread(Planner planner, PlannerDay plannerDay, GenericEvent oldGe, GenericEvent newGe)
        {
            this.oldGe = oldGe;
            this.newGe = newGe;
            this.plannerDay = plannerDay;
            initCalendarThread();
        }

        public CalendarThread(Planner planner, PlannerWeek plannerWeek, GenericEvent oldGe, GenericEvent newGe)
        {
            this.oldGe = oldGe;
            this.newGe = newGe;
            this.plannerWeek = plannerWeek;
            initCalendarThread();
        }

        public CalendarThread(Planner planner, PlannerMonth plannerMonth,GenericEvent oldGe, GenericEvent newGe)
        {
            this.oldGe = oldGe;
            this.newGe = newGe;
            this.plannerMonth = plannerMonth;
            initCalendarThread();
        }

        private void initCalendarThread()
        {
            if (UserProfile.Instance().connection) planner.stateLabel.Text = playing;
        }

        public void initSycThread()
        {
            initCalendarThread();
            if (UserProfile.Instance().connection) CalendarConnection.Instance().syncCalandar();
            viewPlannerDay();
            viewPlannerWeek();
            viewPlannerMonth();
            finish();
        }

        public void addThread()
        {
            if (ge.Title.Length >= 255 || ge.Contents.Length >= 255 || ge.Location.Length >= 255)
            {
                MessageBox.Show("글자수를 초과했습니다.");
                return;
            }
            CalendarDB.Instance().save_db(ge);
            CalendarConnection.Instance().addCalendar(ge);
            finish();
        }

        public void updateThread()
        {
            if (ge.Title.Length >= 255 || ge.Contents.Length >= 255 || ge.Location.Length >= 255)
            {
                MessageBox.Show("글자수를 초과했습니다.");
                return;
            }
            
            CalendarDB.Instance().update_db(oldGe, newGe);

            CalendarConnection.Instance().updateCalendar(oldGe, newGe);

            finish();
        }

        public void delThread()
        {
            CalendarDB.Instance().del_db(ge);

            planner.plannerilist.Remove(planner.plannerList.Items.IndexOf(planner.plannerList.SelectedItem));
            planner.plannerList.Items.Remove(planner.plannerList.SelectedItem);

            if (UserProfile.Instance().connection) CalendarConnection.Instance().delCalendar(ge);

            finish();
        }

        private void finish()
        {
            if (planner != null) viewPlanner();
            if (plannerDay != null) viewPlannerDay();
            if (plannerWeek != null) viewPlannerWeek();
            if (plannerMonth != null) viewPlannerMonth();

            if (UserProfile.Instance().connection)
            {
                planner.stateLabel.Text = complete;
                System.Threading.Thread.Sleep(800);
                planner.stateLabel.Text = nullstr;
                System.Threading.Thread.Sleep(200);
                planner.stateLabel.Text = complete;
                System.Threading.Thread.Sleep(800);
                planner.stateLabel.Text = nullstr;
                System.Threading.Thread.Sleep(200);
                planner.stateLabel.Text = complete;
                System.Threading.Thread.Sleep(800);
                planner.stateLabel.Text = nullstr;
            }
        }

        private void viewPlanner()
        {

        }

        private void viewPlannerMonth()
        {
            plannerMonth.viewAll();
        }

        private void viewPlannerWeek()
        {
            plannerWeek.viewAllDB();
        }

        private void viewPlannerDay()
        {
            plannerDay.viewAllDB();
        }
    }
}
